/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans.search;

import com.easoft.code.pos.App;
import com.easoft.code.pos.data.dao.GenericDao;
import com.easoft.code.pos.data.exception.CoreDataLayerException;
import com.easoft.code.pos.data.pojos.Producto;
import com.easoft.code.pos.data.util.QueryParameter;
import com.easoft.code.pos.ui.models.PojoTableModel;
import com.easoft.code.pos.util.UIUtil;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Eliu
 */
public class FormLovUtil extends javax.swing.JDialog {

    private PojoTableModel searchModel;
    private GenericDao dao = new GenericDao();
    private List<Producto> localProducs;
    private Producto selProduct;

    public FormLovUtil() {
        super(App.mainForm, "Busqueda de Productos", true);
        Frame parent = App.mainForm;
        if (parent != null) {
            Dimension parentSize = parent.getSize();
            Point p = parent.getLocation();
            setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
        }

        initComponents();
        reloadProducts();
        searchModel = new PojoTableModel(localProducs,
                Arrays.asList(new String[]{"Codigo",
                    "Nombre", "Precio Compra"}),
                Arrays.asList(new String[]{"Codigo",
                    "Nombre", "PrecioCosto"}), Producto.class);
        tableResults.setModel(searchModel);

        setSize(new Dimension(500, 300));
        setVisible(true);
    }

    public Producto getSelProduct() {
        return selProduct;
    }

    public void setSelProduct(Producto selProduct) {
        this.selProduct = selProduct;
    }

    private void reloadProducts() {
        try {
            localProducs = (List<Producto>) dao.doNamedQuery("Producto.findAll", new QueryParameter[]{},
                    Producto.class, false);
        } catch (CoreDataLayerException ex) {
            Logger.getLogger(FormLovUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        btnCleanFiltro = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableResults = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();

        setTitle("Busqueda Productos");
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        btnCleanFiltro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-16-delete.png"))); // NOI18N
        btnCleanFiltro.setText("Limpiar");
        btnCleanFiltro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCleanFiltroActionPerformed(evt);
            }
        });
        jPanel3.add(btnCleanFiltro);

        getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_START);

        jPanel1.setLayout(new java.awt.BorderLayout());

        tableResults.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableResults.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableResultsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableResults);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add-4.png"))); // NOI18N
        btnAdd.setText("Agregar");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel2.add(btnAdd);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCleanFiltroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCleanFiltroActionPerformed
        reloadProducts();
        searchModel.fireTableDataChanged();
    }//GEN-LAST:event_btnCleanFiltroActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        if (tableResults.getSelectedRow() < 0) {
            UIUtil.showError("No ha seleccionado ningun producto", "Agregar Producto");
        } else {
            setVisible(false);
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void tableResultsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableResultsMouseClicked
        if (evt.getClickCount() == 2) {
            setSelProduct((Producto) searchModel.getData().get(tableResults.getSelectedRow()));
            btnAddActionPerformed(null);
        }
    }//GEN-LAST:event_tableResultsMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCleanFiltro;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableResults;
    // End of variables declaration//GEN-END:variables
}
